<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: order_details_shipping_preview.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     neommerce - www.neommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require_once(DIR_FS_INC.'inc.get_country_list.php');
require(DIR_FS_INC.'inc.free_shipping.php');

require(DIR_WS_CLASSES.'class.order.php');

$order = new order();
$total_weight = $_SESSION['cart']->show_weight();
$total_count = $_SESSION['cart']->count_contents();
$selected = isset($_SESSION['customer_country_id']) ? $_SESSION['customer_country_id'] : STORE_COUNTRY;

if (!isset($_SESSION['customer_id']) && sizeof(get_countriesList()) > 1) {
	if (isset($_SESSION['country']))
		$selected = $_SESSION['country'];
	else
		$selected = STORE_COUNTRY;
	
	$module_smarty->assign('SELECT_COUNTRY', get_country_list(array('name' => 'country'), $selected, 'onchange="this.form.submit()"'));
}

if(!isset($order->delivery['country']['iso_code_2']) || $order->delivery['country']['iso_code_2'] == '') {
	unset($_SESSION['shipping']);
	$delivery_zone = $db->db_query("SELECT 
										countries_id, 
										countries_iso_code_2, 
										countries_name 
									FROM 
										".TABLE_COUNTRIES." 
									WHERE 
										countries_id = ".$selected);
	$order->delivery['country']['iso_code_2'] = $delivery_zone->fields['countries_iso_code_2'];
	$order->delivery['country']['title'] = $delivery_zone->fields['countries_name'];
	$order->delivery['country']['id'] = $delivery_zone->fields['countries_id'];
}

$_SESSION['delivery_zone'] = $order->delivery['country']['iso_code_2'];
require (DIR_WS_CLASSES.'class.shipping.php');
$shipping = new shipping();
$quotes = $shipping->quote();

$free_shipping = $free_shipping_freeamount = false;
if(defined('MODULE_SHIPPING_FREEAMOUNT_STATUS') && MODULE_SHIPPING_FREEAMOUNT_STATUS == 'True')
	if(($order->info['total'] - $order->info['shipping_cost']) >= $price->format(MODULE_SHIPPING_FREEAMOUNT_AMOUNT,false,0,true))
        $free_shipping = true;
        
$has_freeamount = false;
foreach ($quotes as $quote) {
    if ($quote['id'] == 'freeamount') {
        $has_freeamount = true;
        if (isset($quote['methods'])) {
            $free_shipping_freeamount = true;
            break;
        }
    }
}
include (DIR_WS_LANGUAGES.$_SESSION['language'].'/modules/order_total/ot_shipping.php');

$total = $_SESSION['cart']->show_total();

$shipping_content = array ();
if ($free_shipping == true) {
    $shipping_content[] = array('NAME' => FREE_SHIPPING_TITLE,
        						'VALUE' => $price->format(0, true, 0, true));
        						
} elseif ($free_shipping_freeamount) {
    $shipping_content[] = array(
        'NAME' => $quote['module'] . ' - ' . $quote['methods'][0]['title'],
        'VALUE' => $price->format(0, true, 0, true));
        
} else {
    if ($has_freeamount)
        $module_smarty->assign('FREE_SHIPPING_INFO', sprintf(FREE_SHIPPING_DESCRIPTION, $price->format(MODULE_SHIPPING_FREEAMOUNT_AMOUNT, true, 0, true)));
    
    $i = 0;
    foreach ($quotes AS $quote) {
		if ($quote['id'] != 'freeamount') {
			$quote['methods'][0]['cost'] = $price->CalculateCurr($quote['methods'][0]['cost']);
			$total += ((isset($quote['tax']) && $quote['tax'] > 0) ? $price->AddTax($quote['methods'][0]['cost'],$quote['tax']) : (!empty($quote['methods'][0]['cost']) ? $quote['methods'][0]['cost'] : '0'));
			$shipping_content[$i] = array('NAME' => $quote['module'] . ' - ' . $quote['methods'][0]['title'],
										'VALUE' => $price->format(((isset($quote['tax']) && $quote['tax'] > 0) ? $price->AddTax($quote['methods'][0]['cost'],$quote['tax']) : (!empty($quote['methods'][0]['cost']) ? $quote['methods'][0]['cost'] : '0')), true));
			$i++;
		}
    }
}

unset($_SESSION['delivery_zone']);
$module_smarty->assign('shipping_content', $shipping_content);
$module_smarty->assign('COUNTRY', $order->delivery['country']['title']);

if(count($shipping_content) <= 1)
    $module_smarty->assign('TOTAL', $price->format($total, true));
?>